/**
 * Created by caiqyxyx on 2017/2/8.
 */
(function (win) {
    var getRootPath = function () {
        var curWwwPath = window.document.location.href;
        var pathName = window.document.location.pathname;
        var pos = curWwwPath.indexOf(pathName);
        var localhostPaht = curWwwPath.substring(0, pos);
        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        return (localhostPaht + projectName + "/");
    };
    /*扩展String*/
    var _utf8_encode = function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    };

    /**
     * @returns {string}
     */
    String.prototype.base64encode = function () {
        var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        var input = _utf8_encode(this);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }
        return output;
    };
    /**
     * @param str
     * @returns {boolean}
     */
    String.prototype.endWith = function (str) {
        var reg = new RegExp(str + "$");
        return reg.test(this);
    };
    /**
     * @param str
     * @returns {boolean}
     */
    String.prototype.startWith = function (str) {
        var reg = new RegExp("^" + str);
        return reg.test(this);
    };

    /**
     * 去除url中的'/' 字符
     * @returns {string}
     */
    String.prototype.upgradeUrl = function () {
        return this.replace(/\//g, '');
    };

    win.cywork = {};
    win.cywork.global = {};
    win.cywork.global.rootPath = getRootPath();
    win.cywork.global.humanId = -1;
    win.cywork.global.humanName = '';
    win.cywork.global.unitId = -1;
    win.cywork.global.regionId = -1;
    win.cywork.global.unitName = '';
    win.cywork.global.regionName = '';

    /*添加程序入口*/
    var mainJs = document.createElement("script");
    mainJs.setAttribute("type", "text/javascript");
    mainJs.setAttribute("data-main", win.cywork.global.rootPath + "view/main/main.js?t=" + (new Date()).getTime());
    mainJs.setAttribute("src", win.cywork.global.rootPath + "library/3rdparty/require/require-2.1.11.js?t=" + (new Date()).getTime());
    document.getElementsByTagName("head").item(0).appendChild(mainJs);
})(this);